package com.yx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.dao.OwnerMapper;
import com.yx.dao.RepairMapper;
import com.yx.dao.RepairtypeMapper;
import com.yx.model.Owner;
import com.yx.model.Repair;
import com.yx.model.Repairtype;
import com.yx.model.Tongji;
import com.yx.service.IRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements IRepairService {

    @Autowired
    private RepairMapper repairDao;

    @Autowired
    private OwnerMapper ownerMapper;

    @Autowired
    private RepairtypeMapper repairtypeMapper;

    /**
     * 分页条件查询
     *
     * @param iPage
     * @param repair
     * @return
     */
    @Override
    public IPage<Repair> findRepairAll(IPage<Repair> iPage, Repair repair) {
        // 封装查询条件
        String comId = repair.getComId();
        Integer status = repair.getStatus();
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(comId)) {
            wrapper.eq("com_id", comId);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }

        IPage<Repair> repairIPage = repairDao.selectPage(iPage, wrapper);

        // 进行数据封装
        List<Repair> records = repairIPage.getRecords();
        records = records.stream().map(repair1 -> {
            // 拥有者
            Integer ownerId = repair1.getOwnerId();
            Owner owner = ownerMapper.selectById(ownerId);
            repair1.setOwner(owner);

            // 保修类型
            String comId1 = repair1.getComId();
            Repairtype repairtype = repairtypeMapper.selectById(comId1);
            repair1.setType(repairtype);
            return repair1;
        }).collect(Collectors.toList());
        repairIPage.setRecords(records);
        return repairIPage;
    }

    @Override
    public PageInfo<Repair> findRepairAll(int page, int pagesize, Repair repair) {
        // 封装条件
        Page<Repair> repairPage = new Page<>(page, pagesize);
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        String comId = repair.getComId();
        Integer ownerId = repair.getOwnerId();
        Integer status = repair.getStatus();
        wrapper.eq("owner_id", ownerId);

        if (!StringUtils.isEmpty(comId)) {
            wrapper.eq("com_id", comId);
        }

        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }

        Page<Repair> selectPage = repairDao.selectPage(repairPage, wrapper);
        List<Repair> records = selectPage.getRecords();

        // 进行数据封装
        records = records.stream().map(repair1 -> {
            // 拥有者
            Integer ownerId1 = repair1.getOwnerId();
            Owner owner = ownerMapper.selectById(ownerId1);
            repair1.setOwner(owner);

            // 保修类型
            String comId1 = repair1.getComId();
            Repairtype repairtype = repairtypeMapper.selectById(comId1);
            repair1.setType(repairtype);
            return repair1;
        }).collect(Collectors.toList());
        // 进行返回数据
        PageInfo<Repair> pageInfo = new PageInfo();
        pageInfo.setTotal(selectPage.getTotal());
        pageInfo.setList(records);
        return pageInfo;
    }

    @Override
    public List<Repair> queryList() {
        return repairDao.queryRepairAll(null);
    }


    @Override
    public IPage<Repair> findListByPage(Integer page, Integer pageCount) {
        IPage<Repair> wherePage = new Page<>(page, pageCount);
        Repair where = new Repair();

        return baseMapper.selectPage(wherePage, Wrappers.query(where));
    }

    @Override
    public int add(Repair repair) {
        return baseMapper.insert(repair);
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateData(Repair repair) {
        return baseMapper.updateById(repair);
    }

    @Override
    public Repair findById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<Tongji> queryTongji() {
        return repairDao.queryTongji();
    }
}
